package edu.uoc.tdp.pac3.ui.server;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.uoc.tdp.pac3.server.Server;
import edu.uoc.tdp.pac3.ui.i18n.LanguageUtils;

public class ServerPanel extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;	
	private JButton runButton;
	private JButton stopButton;
	private JLabel labelState;
	
	Server rmiServer = null;
	
	public ServerPanel() {
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		constraints.insets = new Insets(5, 5, 5, 5);
		
		labelState = new JLabel();
		labelState.setText(LanguageUtils.getMessage("server.stopped"));
		constraints.gridx = 0; 
		constraints.gridy = 0; 
		constraints.gridwidth = 2;
		constraints.gridheight = 1;
		constraints.weightx = constraints.weighty = 1.0;
		add(labelState,constraints);
		
		runButton = new JButton(LanguageUtils.getMessage("server.button.run"));
		runButton.addActionListener(this);
		constraints.gridx = 0; 
		constraints.gridy = 1;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		add(runButton,constraints);
		
		stopButton = new JButton(LanguageUtils.getMessage("server.button.stop"));
		stopButton.addActionListener(this);
		constraints.gridx = 1; 
		constraints.gridy = 1; 
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		add(stopButton,constraints);
	}

	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==runButton) {
			//Start server...
			if(rmiServer == null){
				rmiServer = new Server();
				labelState.setText(LanguageUtils.getMessage("server.running"));
				runButton.setEnabled(false);
			}
			/*JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("Panel3.dialog1"),
					LanguageUtils.getMessage("Panel3.dialogsTitle"),
					JOptionPane.INFORMATION_MESSAGE);*/
		}
		else if (e.getSource()==stopButton) {
			if(rmiServer != null){
				rmiServer.stopServer();
				labelState.setText(LanguageUtils.getMessage("server.stopped"));
				runButton.setEnabled(true);
			}
			/*
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("Panel3.dialog2"),
					LanguageUtils.getMessage("Panel3.dialogsTitle"),
					JOptionPane.ERROR_MESSAGE);
					*/
		}
	}

}
